﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Windows.Forms;
using System.IO.Compression;
using System.Drawing;


namespace Candy_Eyes_Pro
{
    class Html
    {
        public string ReadUrl(string url, string referer)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.98 Safari/534.13";
            request.Accept = "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
            request.Headers.Add("Accept-Encoding:gzip");
            request.Headers.Add("Accept-Language:vi-VN,vi;q=0.8,fr-FR;q=0.6,fr;q=0.4,en-US;q=0.2,en;q=0.2");
            request.ContentType = "application/x-www-form-urlencoded";
            request.KeepAlive = true;
            if (referer != string.Empty) request.Referer = referer;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            byte[] b;
            if (response.GetResponseHeader("Content-Encoding").Contains("gzip"))
            {
                b = Html.DecompressGzip(response.GetResponseStream());
                return Encoding.UTF8.GetString(b);
            }
            b = ((MemoryStream)response.GetResponseStream()).ToArray();
            return Encoding.UTF8.GetString(b);
        }
        public string ReadUrl(string url)
        {
            return ReadUrl(url, string.Empty);
        }
        private char[] ToCharArray(byte[] b)
        {
            char[] b1 = new char[b.Length];
            for (int i = 0; i < b.Length; i++)
            {
                b1[i] = (char)b[i];
            }
            return b1;
        }
        public byte[] Download(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.98 Safari/534.13";
            request.Accept = "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
            request.Headers.Add("Accept-Encoding:gzip");
            request.Headers.Add("Accept-Language:vi-VN,vi;q=0.8,fr-FR;q=0.6,fr;q=0.4,en-US;q=0.2,en;q=0.2");
            request.ContentType = "application/x-www-form-urlencoded";
            request.KeepAlive = true;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response.GetResponseHeader("Content-Encoding").Contains("gzip"))
            {
                byte[] b = Html.DecompressGzip(response.GetResponseStream());
                return b;
            }
            MemoryStream memory = (MemoryStream)response.GetResponseStream();
            return memory.ToArray();
        }
        public Image DownloadImg(string url)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                return Image.FromStream(response.GetResponseStream());
            }
            catch
            {
                return null;// Properties.Resources.noimg;
            }
        }
        private static byte[] DecompressGzip(Stream streamInput)
        {
            Stream streamOutput = new MemoryStream();
            int iOutputLength = 0;
            try
            {
                byte[] readBuffer = new byte[4096];

                /// read from input stream and write to gzip stream

                using (GZipStream streamGZip = new GZipStream(streamInput, CompressionMode.Decompress))
                {

                    int i;
                    while ((i = streamGZip.Read(readBuffer, 0, readBuffer.Length)) != 0)
                    {
                        streamOutput.Write(readBuffer, 0, i);
                        iOutputLength = iOutputLength + i;
                    }
                }
            }
            catch (Exception ex)
            {

            }

            /// read uncompressed data from output stream into a byte array

            byte[] buffer = new byte[iOutputLength];
            streamOutput.Position = 0;
            streamOutput.Read(buffer, 0, buffer.Length);

            return buffer;
        }
    }
}
